package fab.forms.fields 
{
	import fab.forms.error.FormError;
	import fab.forms.fields.Element;
	import flash.text.TextField;
	/**
	 * ...
	 * @author fab
	 */
	public class DateElement extends Element
	{
		//private var format:String;
		private var fields:Array;
		
		public function DateElement(na:String, _fields:Array)//jour_id:String, mois_id:String, annee_id:String, format:String ) 
		{
			name = na;
			fields = _fields;
		}
		
		override public function validate():FormError
		{
			var error:FormError;
			if ( hasError( FormError.NONEMPTY ) && !validateNonempty() )
			{
				error = getError( FormError.NONEMPTY );
			}
			else if ( hasError( FormError.YEARLENGTH ) && !validateYearLength() )
			{
				error = getError( FormError.YEARLENGTH );
			}			
			return error;			
		}		
		private function validateNonempty():Boolean
		{
			for each( var f:TextField in fields ) if ( f.text == "" ) return false;
			return true;
		}		
		private function validateYearLength():Boolean
		{
			return (fields[2] as TextField).text.length == 4;
		}			
		override public function getValue():String
		{
			//TODO switch du format ?
			
			var jour:String = ((fields[0] as TextField).text.length == 1)?"0" + (fields[0] as TextField).text:(fields[0] as TextField).text;
			var mois:String = ((fields[1] as TextField).text.length == 1)?"0" + (fields[1] as TextField).text:(fields[1] as TextField).text;
			//var jour:String = ((fields[2] as TextField).text.length() == 1)?"0" + (fields[2] as TextField).text:(fields[2] as TextField).text;
			return (fields[2] as TextField).text + "-" + mois + "-" + jour;
		}		
	}

}